﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iTeachSoft.Presentation.Dialogs;
using iTeachSoft.Code.Managers;
using iTeachSoft.Presentation.Code.Managers;
using iTeachSoft.Presentation.Code.Utility;
using iTeachSoft.Presentation.Code.Wrappers;
using iTeachSoft.CoreObjects;
using iTeachSoft.CoreObjects.Recording;
using System.Drawing;
using iTeachSoft.Presentation.Code.ExtensionClasses;

namespace iTeachSoft.Presentation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.WindowState = WindowState.Maximized;

            AddEventHandlers();
            ReflectToolbarButtons();
        }

        private void ReflectToolbarButtons()
        {
            toolBarEdit.IsEnabled = true;
            toolBarInsert.IsEnabled = true;
            toolBarRecord.IsEnabled = true;

            buttonImportFrame.IsEnabled = true;
            buttonSaveProject.IsEnabled = true;
            buttonInsertSound.IsEnabled = true;
            buttonCompileAvi.IsEnabled = true;

            if (RuntimeStorage.CurrentProject == null)
            {
                toolBarEdit.IsEnabled = false;
                toolBarInsert.IsEnabled = false;
                toolBarRecord.IsEnabled = false;

                buttonImportFrame.IsEnabled = false;
                buttonSaveProject.IsEnabled = false;
                buttonInsertSound.IsEnabled = false;
                buttonCompileAvi.IsEnabled = false;
            }
        }

        private void ReflectProjectTreeView()
        {
            TreeViewItem rootItem = new TreeViewItem() { Header = RuntimeStorage.CurrentProject.Name, IsExpanded = true, Style = (Style)Application.Current.FindResource("TreeViewItemShapeStyle") };
            foreach (Slide slide in RuntimeStorage.CurrentProject.Slides)
            {
                TreeViewItem slideItem = new SlideTreeViewItem(slide);
                foreach (BaseSlideObject slideObject in slide.SlideObjects)
                {
                    TreeViewItem slideObjectItem = new ObjectTreeViewItem(slideObject, slide);
                    slideItem.Items.Add(slideObjectItem);
                }
                rootItem.Items.Add(slideItem);
            }

            treeViewProject.Items.Clear();
            treeViewProject.Items.Add(rootItem);
        }

        private void AddEventHandlers()
        {
            this.miExit.Click += new RoutedEventHandler(miExit_Click);
            this.Closed += new EventHandler(MainWindow_Closed);

            Application.Current.MainWindow.SizeChanged += new SizeChangedEventHandler(MainWindow_SizeChanged);

            this.miNewProject.Click += new RoutedEventHandler(miNewProject_Click);
            this.buttonNewProject.Click += new RoutedEventHandler(buttonNewProject_Click);

            this.buttonImportFrame.Click += new RoutedEventHandler(buttonImportFrame_Click);
            this.miInsertExistingFrame.Click += new RoutedEventHandler(miInsertExistingFrame_Click);

            this.miInsertAnnotation.Click += new RoutedEventHandler(miInsertAnnotation_Click);
            this.buttonInsertAnnotation.Click += new RoutedEventHandler(buttonInsertAnnotation_Click);

            this.miInsertRectangle.Click += new RoutedEventHandler(miInsertRectangle_Click);
            this.buttonInsertTextAnnotation.Click += new RoutedEventHandler(buttonInsertTextAnnotation_Click);

            this.miInsertSound.Click += new RoutedEventHandler(miInsertSound_Click);
            this.buttonInsertSound.Click += new RoutedEventHandler(buttonInsertSound_Click);

            this.miExportAsAVI.Click += new RoutedEventHandler(miExportAsAVI_Click);
            this.buttonCompileAvi.Click += new RoutedEventHandler(buttonCompileAvi_Click);

            this.buttonRecord.Click += new RoutedEventHandler(buttonRecord_Click);
            this.miRecord.Click += new RoutedEventHandler(miRecord_Click);

            this.buttonStopRecording.Click += new RoutedEventHandler(buttonStopRecording_Click);
            this.miStopRecording.Click += new RoutedEventHandler(miStopRecording_Click);

            this.miOptions.Click += new RoutedEventHandler(miOptions_Click);

            //Application events
            EventsManagement.Instance.ReflectProjectInvoked += new EventHandler(Instance_ReflectProject);
            EventsManagement.Instance.ReflectTreeViewRequested += new EventHandler(Instance_ReflectTreeViewRequested);
            EventsManagement.Instance.RecordingEnded += new EventHandler<SingleItemEventArgs<System.Drawing.Bitmap[]>>(Instance_RecordingEnded);
        }

        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            accordionItemProjectExplorer.Measure(new System.Windows.Size());
            accordionItemProjectExplorer.UpdateLayout();
        }

        #region "Application events handler"

        void Instance_ReflectProject(object sender, EventArgs e)
        {
            ReflectToolbarButtons();
            ReflectProjectTreeView();
            controlFilmStrip.ReflectCurrentProject();
        }

        void Instance_ReflectTreeViewRequested(object sender, EventArgs e)
        {
            ReflectProjectTreeView();
        }

        void Instance_RecordingEnded(object sender, SingleItemEventArgs<System.Drawing.Bitmap[]> e)
        {
            foreach (Bitmap image in e.Item)
            {
                Slide newSlide = new Slide();
                newSlide.PreserveRation = true;

                //the values in the text boxes should be correct
                newSlide.Size = RuntimeStorage.CurrentProject.SlidesSize;
                newSlide.ParentProject = RuntimeStorage.CurrentProject;

                newSlide.SaveSlideImageToDisk(image);

                RuntimeStorage.CurrentProject.Slides.Add(newSlide);
            }

            EventsManagement.Instance.OnReflectProjectInvoked(this, EventArgs.Empty);
            //Select the newly added slide
            EventsManagement.Instance.OnSelectedSlideIndexChanged(this, new SingleItemEventArgs<int>(RuntimeStorage.CurrentProject.Slides.Count - 1));

        }

        #endregion

        #region "Controls event handlers"

        void miStopRecording_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.StopRecording();
        }

        void buttonStopRecording_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.StopRecording();
        }

        void miRecord_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.StartRecording();
        }

        void buttonRecord_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.StartRecording();
        }

        void buttonInsertAnnotation_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertAnnotation();
        }

        void miInsertAnnotation_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertAnnotation();
        }

        void buttonInsertTextAnnotation_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertTextAnnotation();
        }

        void miInsertRectangle_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertTextAnnotation();
        }

        void miOptions_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.ShowProjectOptions();
        }

        void buttonCompileAvi_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.ExportPresentationAsAVI();
        }

        void miExportAsAVI_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.ExportPresentationAsAVI();
        }

        void buttonInsertSound_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertAudioTrack(this.slideViewControl.CurrentSlide);
        }

        void miInsertSound_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertAudioTrack(this.slideViewControl.CurrentSlide);
        }

        void miInsertExistingFrame_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertNewFrame();
        }

        void buttonImportFrame_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.InsertNewFrame();
        }

        void buttonNewProject_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.NewProject();
        }

        void miNewProject_Click(object sender, RoutedEventArgs e)
        {
            ActionsManager.NewProject();
        }

        private void miExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        #endregion

    }
}
